package com.ruoyi.book.service.impl;

import java.util.Calendar;
import java.util.List;

import com.ruoyi.book.mapper.TBookMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.book.mapper.TLeftMapper;
import com.ruoyi.book.domain.TLeft;
import com.ruoyi.book.service.ITLeftService;

/**
 * 图书借阅表Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-04
 */
@Service
public class TLeftServiceImpl implements ITLeftService {
    @Autowired
    private TLeftMapper tLeftMapper;

    @Autowired
    private TBookMapper tBookMapper;

    /**
     * 查询图书借阅表
     *
     * @param id 图书借阅表主键
     * @return 图书借阅表
     */
    @Override
    public TLeft selectTLeftById(Long id) {
        return tLeftMapper.selectTLeftById(id);
    }

    /**
     * 查询图书借阅表列表
     *
     * @param tLeft 图书借阅表
     * @return 图书借阅表
     */
    @Override
    public List<TLeft> selectTLeftList(TLeft tLeft) {
        return tLeftMapper.selectTLeftList(tLeft);
    }

    /**
     * 新增图书借阅表
     *
     * @param tLeft 图书借阅表
     * @return 结果
     */
    @Override
    public int insertTLeft(TLeft tLeft) {
        Long userId = SecurityUtils.getUserId();
        tLeft.setUserId(userId);
        tLeft.setBorrowTime(DateUtils.getNowDate());
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(DateUtils.getNowDate());
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.MONTH, 1);
        tLeft.setReturnTime(calendar.getTime());
        tLeft.setIsLeft("0");
        int i = tLeftMapper.insertTLeft(tLeft);
        if (i > 0) {
            // 图书借出时 数量减一
            tBookMapper.reduceSurplus(tLeft.getBookId());
        }
        return i;
    }

    /**
     * 修改图书借阅表
     *
     * @param tLeft 图书借阅表
     * @return 结果
     */
    @Override
    public int updateTLeft(TLeft tLeft) {
        tLeft.setBorrowTime(DateUtils.getNowDate());
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(DateUtils.getNowDate());
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.MONTH, 1);
        tLeft.setReturnTime(calendar.getTime());
        return tLeftMapper.updateTLeft(tLeft);
    }

    /**
     * 修改图书借阅表
     *
     * @param tLeft 图书借阅表
     * @return 结果
     */
    @Override
    public int returnBook(TLeft tLeft) {
        tLeft.setIsLeft("1");
        int i = tLeftMapper.updateTLeft(tLeft);
        if (i > 0) {
            TLeft left = tLeftMapper.selectTLeftById(tLeft.getId());
            tBookMapper.addSurplus(tLeft.getBookId());
        }
        return i;
    }

    /**
     * 批量删除图书借阅表
     *
     * @param ids 需要删除的图书借阅表主键
     * @return 结果
     */
    @Override
    public int deleteTLeftByIds(Long[] ids) {
        return tLeftMapper.deleteTLeftByIds(ids);
    }

    /**
     * 删除图书借阅表信息
     *
     * @param id 图书借阅表主键
     * @return 结果
     */
    @Override
    public int deleteTLeftById(Long id) {
        return tLeftMapper.deleteTLeftById(id);
    }
}
